package com.gko100.dao.impl;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.gko100.model.Disease;
import com.gko100.dao.ArticleDao;
import com.gko100.dao.basehibernatesupport.BaseHibernateDaoSupport;

import com.gko100.model.Article;
import com.gko100.model.Language;
import com.gko100.model.Navigation;

public class ArticleDaoHibernate extends BaseHibernateDaoSupport implements ArticleDao {

	public void delete(Article arti) {

		this.getHibernateTemplate().delete(arti);
		
	}

	public void delete(Integer artiId) {
		
		this.getHibernateTemplate().delete(this.get(artiId));
	}
	public List<Disease> findDiseListByArticle(Article arti){
		
		String hql = "select a.diseSet from Article as a where a.artiId=?";
		List<Disease> diseList = this.getHibernateTemplate().find(hql, arti.getArtiId());
		if(diseList!=null&&diseList.size()>=1){
			
			return diseList;
		}else{
			
			return null;
		}
		
	}
	public Article findArtiByName(String name) {
		
		List<Article> artiList=this.getHibernateTemplate().find("from Article as a where a.deleteFlag=false and a.artiName=?",name);
		if(artiList!=null&&artiList.size()>=1){
			return artiList.get(0);
		}else{
			
			return null;
		}
	}
	//前台---分类文章总数
	public long find_artiTotalCountByNav(List<Navigation> navList){
		
		Object[] values = new Object[navList.size()];
		String hql_param = "";
		int i=0;
		while(i<navList.size()){
			values[i] = navList.get(i);
			if(i==navList.size()-1){
				hql_param += "a.nav=?"; 
			}else{
				hql_param += "a.nav=? or "; 
			}
			i++;
		}
		String hql = "select count(*) from Article as a where a.deleteFlag=false and ("+hql_param+") order by a.addDate";
		long artiTotalCount = (Long) this.getHibernateTemplate().find(hql, values).listIterator().next();
		values = null;
		hql_param = null;
		return artiTotalCount;
	}
	public long find_backArtiTotalCountByNav(List<Navigation> navList){
		
		
		Object[] values = new Object[navList.size()];
		String hql_param = "";
		int i=0;
		while(i<navList.size()){
			values[i] = navList.get(i);
			if(i==navList.size()-1){
				hql_param += "a.nav=? "; 
			}else{
				hql_param += "a.nav=? or "; 
			}
			i++;
		}
		
		String hql = "select count(*) from Article as a where "+hql_param+"order by a.addDate";
		long artiTotalCount = this.findByParam(hql, values);
		values = null;
		return artiTotalCount;
	}
	public List<Article> findBackArtiListByNavAndPage(List<Navigation> navList, int offset, int pageSize){
		
		
		Object[] values = new Object[navList.size()];
		String hql_param = "";
		int i=0;
		while(i<navList.size()){
			values[i] = navList.get(i);
			if(i==navList.size()-1){
				hql_param += "a.nav=? "; 
			}else{
				hql_param += "a.nav=? or "; 
			}
			i++;
		}
		String hql = "from Article as a where "+ hql_param +"order by a.addDate desc";
		List<Article> artiList = this.findByPage(hql, values, offset, pageSize);
		values = null;
		if(artiList!=null&&artiList.size()>=1){	
		
			return artiList;
		}else{

			return null;
		}
		
	}
	
	public List<Article> findArtiFront(Navigation nav, Integer artiId, int offset, int pageSize){
		
		String hql = "from Article as a where a.deleteFlag=false and a.nav=? and a.artiId<? order by a.addDate desc";
		
		List<Article> artiList = this.findByPage(hql, nav, artiId, offset, pageSize);
		
		if(artiList!=null&&artiList.size()>=1){	
			
			return artiList;
		}else{

			return null;
		}
	}
	public List<Article> findArtiNext(Navigation nav, Integer artiId, int offset, int pageSize){
		
		String hql = "from Article as a where a.deleteFlag=false and a.nav=? and a.artiId>? order by a.addDate";
		
		List<Article> artiList = this.findByPage(hql, nav, artiId, offset, pageSize);
		
		if(artiList!=null&&artiList.size()>=1){	
		
			return artiList;
		}else{

			return null;
		}
	}

	//前台---查找分类下所有文章---分页
	public List<Article> findArtiListByNavListAndPage(List<Navigation> navList, int offset, int pageSize){
		
		Object[] values = new Object[navList.size()];
		String hql_param = "";
		int i=0;
		while(i<navList.size()){
			values[i] = navList.get(i);
			if(i==navList.size()-1){
				hql_param += "a.nav=?"; 
			}else{
				hql_param += "a.nav=? or "; 
			}
			i++;
		}
		String hql = "from Article as a where a.deleteFlag=false and ("+ hql_param +") order by a.addDate desc";
		List<Article> artiList = this.findByPage(hql, values, offset, pageSize);
		values = null;
		if(artiList!=null&&artiList.size()>=1){	
			return artiList;
		}else{

			return null;
		}
		
		
	}
	
	public List<Article> findArtiListByNavAndPage(Navigation nav, int offset, int pageSize){
		
		String hql = "from Article as a where a.deleteFlag=false and a.nav=? order by a.addDate";
		
		List<Article> artiList = this.findByPage(hql, nav, offset, pageSize);
		
		if(artiList!=null&&artiList.size()>=1){	
			return artiList;
		}else{

			return null;
		}
		
	}
	public List<Article> findArtiListByNav(Navigation nav) {
		
		final Navigation n = nav;
		final String hql = "from Article as a where a.deleteFlag=false and a.nav=?";
		List<Article> artiList= this.findByParam(hql, nav, 0, 5);

		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}else{
			
			
			return null;
		}
	}
	
	
	public List<Article> findArtiListByNavAndSortParam(Navigation nav, String sortParam){
		
		String sql = "";
		if(sortParam.equals("ASC")){
			
			sql = "from Article as a where a.deleteFlag=false and a.nav=? order by a.artiId ASC";
		}else if(sortParam.equals("DESC")){
			
			sql = "from Article as a where a.deleteFlag=false and a.nav=? order by a.artiId DESC";
		}
		
		List<Article> artiList = this.getHibernateTemplate().find(sql, nav);
		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}
		
		return null;

	}
	
	public List<Article> findArtiListByNav(Navigation nav,int pageNo, int pageSize){
		
		if(pageNo<1){
			return null;
		}
		int offset=(pageNo-1)*pageSize;
		List<Article> artiList=this.findByPage("from Article as a where a.nav=? and a.deleteFlag=false",nav, offset, pageSize);
		
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}
	
	public List<Article> findArtiListByNavIdAndArtiKey(Integer navId,
			String artiKey) {
		
		List<Article> artiList=this.getHibernateTemplate().find("from Article as a where a.navid=? and a.artiKey=? and a.deleteFlag=false",navId,artiKey);
		return null;
	}
	
	public Article findBackArtiByArtiId(Integer artiId){
		
		Article arti=this.getHibernateTemplate().get(Article.class, artiId);
		if(arti!=null){
			return arti;
		}{
			
			return null;
		}
	}
	
	public Article get(Integer artiId) {
		
		Article arti=this.getHibernateTemplate().get(Article.class, artiId);
		if(arti!=null&&!arti.getDeleteFlag()){
			return arti;
		}{
			
			return null;
		}
	
	}

	public void save(Article arti) {
		
		this.getHibernateTemplate().save(arti);
		
	}

	public void update(Article arti) {
		
		this.getHibernateTemplate().update(arti);
		
	}

	public int getArtiCount(Navigation nav){
		
		if(this.findArtiListByNav(nav)==null){
			return 0;
		}else{
			return this.findArtiListByNav(nav).size();
		}
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public Navigation findNavByArtiId(Integer artiId) {
		
		List<Navigation> navList = this.getHibernateTemplate().find("select a.nav from Article as a where a.deleteFlag=false and a.artiId=?",artiId);
		if(navList!=null&&navList.size()>=1){
			return navList.get(0);
		}else{
			
			return null;
		}
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Article> findArtiListByNavAndLangAndASC(Navigation nav, Language lang) {
		
		
		List<Article> artiList = this.getHibernateTemplate().find("from Article as a where a.deleteFlag=false and a.nav=? and a.lang=? order by a.artiId asc",nav,lang);
		
		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}else{
			
			return null;
		}
		
	}
	public List<Article> findArtiListByNavAndLangAndDESC(Navigation nav, Language lang) {
		
		
		List<Article> artiList = this.getHibernateTemplate().find("from Article as a where a.deleteFlag=false and a.nav=? and a.lang=? order by a.artiId desc",nav,lang);
		
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
		
	}
	
	
	//前台页面--新闻报道---查询前5条新闻
	public List<Article> findArtiListByNavAndLang(Navigation nav, Language lang, String sortParam){
		
		
		String hql = "";
		if(sortParam.equals("ASC")){
			
			hql = "from Article as a where a.deleteFlag=false and a.nav=? and a.lang=? order by a.artiId asc";
			
		}else if(sortParam.equals("DESC")){
			
			hql = "from Article as a where a.deleteFlag=false and a.nav=? and a.lang=? order by a.artiId desc";
		
		}else{
			
			hql = "from Article as a where a.deleteFlag=false and a.nav=? and a.lang=? order by a.artiId asc";
			
		}
		Object[] values = new Object[]{nav, lang};
		
		List<Article> artiList = this.findByParam(hql, values, 0, 5);
		values = null;
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
		
	}
	
	public List<Article> find_artiListBySearchKey(String searchKey){
		
		
		List<Article> artiList = this.getHibernateTemplate().find("from Article as a " +
				"where a.deleteFlag=false and (a.artiTitle like '%"+searchKey+"%' or a.artiKey like '%"+searchKey+"%') order by a.addDate");
		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}else{
			
			return null;
		}
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Article> findArtiListByDise(Disease dise) {
		
		List<Article> artiList=this.getHibernateTemplate().find("from Article as a where a.deleteFlag=false and a.dise=? order by a.addDate desc",dise);
		
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
	}

	public List<Article> findArtiListByDisAndNav(List<Disease> diseList, Navigation nav){
		
		Object[] values = new Object[diseList.size()+1];
		values[0] = nav;
		String hql_param = "";
		int i=1;
		while(i<diseList.size()+1){
			values[i] = diseList.get(i-1).getDiseaseId();
			
			if(i==diseList.size()){
				hql_param += "d.diseaseId=?"; 
			}else{
				hql_param += "d.diseaseId=? or "; 
			}
			i++;
		}
		String hql = "select a from Article as a inner join a.diseSet as d where a.deleteFlag=false and a.nav=? and (" + hql_param+ ") order by a.addDate desc";
		
		List<Article> artiList=this.findByParam(hql, values, 0, 7);
		values = null;
		if(artiList!=null&&artiList.size()>=1){
			
			HashSet hs = new HashSet(artiList);
			artiList.clear();
			artiList.addAll(hs);
			return artiList;
		}else{
			return null;
		}
	}
	public List<Article> findArtiListByDisListAndNote(List<Disease> diseList, String note){
		
		Set<Disease> diseSet = new HashSet<Disease>();
		
		String hql = "select d.artiSet from Disease as d where d.";
		List<Article> artiList=this.getHibernateTemplate().find("from Article as a where a.deleteFlag=false and a.diseSet=? and a.artiNote=? order by a.artiPrio DESC",diseSet, note);
		
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
		
		
	}
	
	public List<Article> findArtiListByDisAndNote(Disease dise, String note){
		
		
		String hql = "select a from Article as a join a.diseSet as d where d.diseaseId=? and a.deleteFlag=false and a.artiNote=? order by a.addDate desc";
		Object[] values = new Object[2];
		values[0] = dise.getDiseaseId();
		values[1] = note;
		List<Article> artiList=this.findByPage(hql, values, 0, 3);
		values = null;
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
	}
	
	public long find_totalCountBySearchKey(String searchKey){
		
		String sql = "select count(*) from Article as a where a.deleteFlag=false and (a.artiTitle like '%"+searchKey+"%' or a.artiKey like '%"+searchKey+"%')";
		long totalCountBySearchKey = (Long) this.getHibernateTemplate().find(sql).listIterator().next();
		return totalCountBySearchKey;
		
	}
	
	
	public List<Article> find_artiListBySearchKeyAndPage(String searchKey, int offset, int pageSize){
		
		
		String sql = "from Article as a where a.deleteFlag=false and (a.artiKey like '%"+searchKey+"%') order by a.addDate";
		
		List<Article> artiList = this.findByPage(sql, offset, pageSize);
		
		if(artiList!=null&&artiList.size()>=1){
			
			return artiList;
		}else{
			
			
			return null;
		}
		
		
	}
	
	
	
	


}
